<?php
class TradeAction extends SystemAction {
	const TRADE_STATUS_UNSHIPPED 		= 1;	//待发货
	const TRADE_STATUS_SHIPPED 			= 2;	//已发货
	const TRADE_STATUS_PENDING 			= 3;	//待处理
	const TRADE_STATUS_PENDINGCANCELD 	= 4;	//取消处理
	
	
	/**
	 * 订单
	 * $status 1:未发货   2 已发货  3 待处理  
	 */
	function tlist() {
		$status = intval ( $_GET ['status'] );
		$tplid = intval ( $_GET ['tplid'] );
		if(! in_array ( $status  , array ( self::TRADE_STATUS_UNSHIPPED, self::TRADE_STATUS_SHIPPED, self::TRADE_STATUS_PENDING, self::TRADE_STATUS_PENDINGCANCELD ) ) ) exit('状态值错误');
		if( $status == 1 || $status == 2 ) {
			if( $tplid && $status == 1 ) {
				$where = " AND systplid = {$tplid}";
			}
			if( $status == 2 ) {
				
				$sellers = D("Seller")->getField('nick', true);
				$this->assign( 'sellers', $sellers );
			}
		$count = D ( 'Trade' )->where ( "sysstatus = {$status} $where" )->count ();
		import ( "ORG.Util.Page" ); // 导入分页类
		$p = new Page ( $count, $this->pagesize );
		$tradelist = D ( 'Trade' )->where ( "sysstatus = {$status} $where" )->limit ( $p->firstRow . ',' . $p->listRows )->order ( "sysaddtime DESC" )->select ();
		}elseif( $status == 3 ){
			$count = D ( 'Trade' )->where ( "sysstatus = 2 AND systodo=1 " )->count ();
			import ( "ORG.Util.Page" ); // 导入分页类
			$p = new Page ( $count, $this->pagesize );
			$tradelist = D ( 'Trade' )->where("sysstatus = 2 AND systodo=1 ")->limit ( $p->firstRow . ',' . $p->listRows )->order ( "sysaddtime DESC" )->select ();
		}else {
			$count = D ( 'TradeView' )->count ();
			import ( "ORG.Util.Page" ); // 导入分页类
			$p = new Page ( $count, $this->pagesize );
			$tradelist = D ( 'TradeView' )->limit ( $p->firstRow . ',' . $p->listRows )->order ( "addtime DESC" )->select ();
		}
		/*
		if( 1 == $status ) {
			return '';
			$tc = new TopClient();
			foreach( $tradelist as $k=>$tmporder ) {
				//$tids .= $tmporder['tid'].';';
			
				$params = array (
						'method' => 'taobao.trade.fullinfo.get',
						'session' => $this->_top_session,
						'timestamp' => date ( 'Y-m-d H:i:s' ),
						'format' => TAO_RETURN_FORMAT,
						'app_key' => TAO_APPKEY,
						'v' => '2.0',
						'sign_method' => 'md5',
			
						'fields' => 'status,orders.logistics_company,orders.invoice_no',
						'tid' => $tmporder['tid']
				);
				$fullinfo = $tc->getResult( $params );
				
				$tradelist[$k]['fullinfo'] 	= 	$fullinfo['trade_fullinfo_get_response']['trade'];
			}
			
			
		}
		*/
		
		$templates = D('Postsetting')->getTemplates( 1 );
		foreach ( $templates as $template) {
			$tploptions .= "<option value='{$template['id']}'>{$template['tplname']}</option>";
			$checked = $tplid == $template['id'] ? "selected" : "";
			$tplsearch  .= "<option value='". U("Trade/tlist?status=1&tplid={$template['id']}")."' {$checked}>". $template['tplname']. "</option>";
		}
		
		
		
		
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign ( 'tradelist', $tradelist );
		$this->assign("status", self::TRADE_STATUS_SHIPPED);
		
		$this->assign( 'tploptions', $tploptions );
		$this->assign( 'tplsearch', $tplsearch );
	
		$this->display ('Trade:tlist'. $status);
		
		//print_r($tradelist);
	}
	function tsearch( ) {
		$map[field] = $_POST['field'] ? htmlspecialchars( $_POST['field'] ) : htmlspecialchars( $_GET['field'] );
		$map[k] = $_POST['k'] ? trim( htmlspecialchars( $_POST['k'] ) ) : trim( htmlspecialchars( $_GET['k'] ) );
		$map[tplid] = $_POST[tplid] ? intval( $_POST[tplid] ) : intval( $_GET[tplid] );
		//时间类型
		$map[timeflag] = $_POST['timeflag'] ? $_POST['timeflag'] : $_GET['timeflag'] ;
		
		$map[time] = $_POST[time] ? htmlspecialchars( $_POST[time]) : '';
		$map[seller_nick] = $_POST[seller_nick] ? htmlspecialchars( $_POST[seller_nick]) : '';
		if( $map[field]  == 'tplid' && $map[tplid] ) {
			$wherestr = " AND systplid='{$map[tplid]}' ";
		}else {
			$hwhere = $_GET['field'] == 'seller_nick'  ? "='{$map[k]}'" : "like '%{$map[k]}%'";
			$wherestr = $map[k] ? " AND {$map[field]}  {$hwhere} " : "" ;
		}
		if( $map[time] ) {
			$wherestr .= " AND DATE_FORMAT( {$map[timeflag]} , '%Y-%m-%d')  = '{$map[time]}' ";
		}
// 		if( $map[seller_nick] ){
// 			$wherestr .= " AND seller_nick= '{$map[seller_nick]}' ";
// 		}
		$status = $_POST[status] ? intval( $_POST[status] ) : intval( $_GET[status] );
		$count = D ( 'Trade' )->where ( "sysstatus = {$status} {$wherestr} ")->count ();
		import ( "ORG.Util.Page" ); // 导入分页类
		$p = new Page ( $count, $this->pagesize );
		$tradelist = D ( 'Trade' )->where ( "sysstatus = {$status} {$wherestr} ")->limit ( $p->firstRow . ',' . $p->listRows )->order ( "sysaddtime DESC" )->select ();
		
		#echo D()->_sql();
		
		$templates = D('Postsetting')->getTemplates( 1 );
		foreach ( $templates as $template) {
			$tploptions .= "<option value='{$template['id']}'>{$template['tplname']}</option>";
		}
		$this->assign( 'tploptions', $tploptions );
		
		
		$sellers = D("Seller")->getField('nick', true);
		$this->assign( 'sellers', $sellers );
		
		$page = $p->show ();
		$this->assign( 'map', $map );
		$this->assign('status', $status );
		$this->assign ( "page", $page );
		$this->assign( 'tradelist', $tradelist);
		$this->assign('time', $map['time']);
		$this->display("tlist2");
		
	}
	//系统发货
	public function tsend() {
		$tid = $_POST ['tid'];
		$company_code = $_POST ['company_code'];
		$out_sid = $_POST ['out_sid']; // 物流公司运单号
		$params = array (
				'method' => 'taobao.logistics.offline.send',
				'session' => $this->_top_session,
				'timestamp' => date ( 'Y-m-d H:i:s' ),
				'format' => TAO_RETURN_FORMAT,
				'app_key' => TAO_APPKEY,
				'v' => '2.0',
				'sign_method' => 'md5',
				'tid' => $tid,
				'out_sid' => $out_sid,
				'company_code' => $company_code 
		);
		
		$tc = new TopClient ();
		$result = $tc->getResult ( $params );
		#error_log( var_export($result, true) ,3,'hgc.log' );
		if ($result ['error_response']) {
			$this->error ( $result ['error_response'] ['sub_msg'] );
		} else if ($result ['delivery_offline_send_response'] ['shipping'] ['is_success']) {
			
			//更新运单号
			$data['out_sid'] = $out_sid ;
			$data['sysstatus'] = self::TRADE_STATUS_SHIPPED ;
			D('Trade')->where("tid={$tid}")->save($data);
			// 记录到日志
			$vo ['tid'] = $tid;
			$vo ['loguser'] = $_SESSION ['seller_nick'];
			$vo ['logtext'] = '系统发货';
			$vo ['logtime'] = date ( 'Y-m-d H:i:s' );
			$vo ['role'] =  'administrator' ;
			D ( 'Tradelog' )->add ( $vo );
			
			$this->ajaxReturn ( $result, '发货成功，订单状态变更为 [卖家已发货] ', 1 );
			
		} else {
			$this->error ( '未知错误' );
		}
	}
	
	function tsendall() {
		$tidall = $_POST ['tid'];
		
		if(! is_array( $tidall)) {
			$tidall = array( 0 => $_POST ['tid'] );
		}
		if( empty( $tidall ) ) $this->error( '未提交订单' );
		
		$tc = new TopClient();
		#Log::write( var_export( $tidall, true) , LOG::INFO);
		foreach ( $tidall as $tid ) {
			$tradeinfo  = D('Trade')->where( "tid='$tid'")->find();
			$seller = D('Sellersetting')->getSellerByNick( $tradeinfo['seller_nick'] );
			$access_token = $seller['access_token'];
			if( empty( $access_token ) ) {
				$error .= "ERR:{$tid} [卖家{$tradeinfo['seller_nick']}未授权]<br>" ;
				continue;				
			}
			$tids = $tradeinfo['systids'];
			$sendtid = $tradeinfo['systids'] ? $tradeinfo['systids'] : $tid ;
			if( empty($tradeinfo ) ) {	
				$error = "ERR:{$tid} [订单错误]<br>";
			}
			$company = D('Company')->where("id={$tradeinfo ['syscomid']}")->find();
			$company_code = $company['code'];
			$out_sid = $tradeinfo ['out_sid']; // 物流公司运单号
			if( empty( $out_sid) ) {
				$error .= "ERR:{$tid} [系统未提供运单号]<br>" ;
				continue;
			}
		
		
			
			//检查订单状态
			//foreach( $tradelist as $k=>$tmporder ) {
			//$tids .= $tmporder['tid'].';';
		
			$params = array (
					'method' => 'taobao.trade.fullinfo.get',
					'access_token' => $access_token ,
					'format' => TAO_RETURN_FORMAT,
					 
					'v' => '2.0',
					 
		
					'fields' => 'status,orders.logistics_company,orders.invoice_no',
					'tid' => $tid,
			);
			Log::write( 'HTTPS:trade_fullinfo_get_params::::'.var_export( $params, true), LOG::INFO );
			$fullinfo = $tc->getHttpsResult( $params );
				
			//$tradelist[$k]['fullinfo'] 	= 	$fullinfo['trade_fullinfo_get_response']['trade'];
			$tradeapiinfo = $fullinfo['trade_fullinfo_get_response']['trade'];
			//	}
			
			//print_r( $tradeapiinfo );
			//检查状态
			if( $tradeapiinfo[status] != 'WAIT_SELLER_SEND_GOODS' ){
				$d['status'] = $tradeapiinfo[status];
				D('Trade')->where("tid={$tid}")->save($d);
				$error .= "ERR:{$tid} [订单状态为 {$tradeapiinfo[status]}]<br>";
				Log::write( 'HTTPS:trade_fullinfo_get_response::::'.var_export( $fullinfo, true), LOG::INFO );
				continue;
			}else {
				//可以发货
				for( $i=0;$i<3;$i++) {
					$params = array();
					if( $tids ) {
						$tids = explode(',', $tids );
						foreach ($tids as $tidTmp ) {
							for( $j=0; $j<3; $j++ ) {
								$params = array (
										'method' => 'taobao.logistics.offline.send',
										'access_token' => $access_token ,
										'format' => TAO_RETURN_FORMAT,										
										'v' => '2.0',
									
										'tid' => $tidTmp,
										'out_sid' => $out_sid,
										'company_code' => $company_code
								);
								$result = $tc->getHttpsResult ( $params );
								Log::write( 'TRADE_SEND_HTTPS::::'.var_export( $result, true).'_i:'.$i.'_tid:'.$tidTmp.'--tids:'. $tradeinfo['systids'] .'_user:'.$_SESSION['seller_nick'], LOG::INFO );
								if(! empty( $result ) ) break;
								
							}
							
						}
						
					}else {
						$params = array (
								'method' => 'taobao.logistics.offline.send',
								'access_token' => $access_token ,
								'format' => TAO_RETURN_FORMAT,				
								'v' => '2.0',
			
								'tid' => $tid,
								'out_sid' => $out_sid,
								'company_code' => $company_code
						);
						$result = $tc->getHttpsResult ( $params );
						if(! empty( $result ) ) break;
						Log::write( 'TRADE_SEND_HTTPS::::'.var_export( $result, true).'_i:'.$i.'_tid:'.$tid.'_user:'.$_SESSION['seller_nick'], LOG::INFO );
					}
					
					
					
				}
				 
				Log::write( 'TRADE_SEND_RESULT::::'.var_export( $result, true), LOG::INFO );
					
				if ($result ['error_response']) {
					$error .= "ERR:{$tid}:{$result ['error_response'] ['msg']} <br>" ;
					
				} else if ($result ['delivery_offline_send_response'] ['shipping'] ['is_success']) {
				//print_r( $result );
					//更新运单号
					// 记录到日志
					$vo ['tid'] = $tid;
					$vo ['loguser'] = $_SESSION ['seller_nick'];
					$vo ['logtext'] = '卖家发货';
					$vo ['logtime'] = date ( 'Y-m-d H:i:s' );
					$vo ['role'] =  $_SESSION['role'] ;
					D ( 'Tradelog' )->add ( $vo );
						
					$success .= "OK:{$tid} [卖家已发货]<br>";
						
				} else {
					#$this->error ( '未知错误' );
				}
					
			}
	
		}
		$this->ajaxReturn( '', $error . $success , 1 );
			
	
	}
	
	
	/**
	 * 保存运单号
	 * 状态更新为 已发货
	 */
	function tsidsave() {
		$tids 		= $_POST[tid];
		$out_sids  	= $_POST[out_sid];
		$listtid 	= $_POST[listtid];
		if( empty( $tids ) || empty( $out_sids ) ){
			$this->error( '订单参数为空' );
		}
		foreach( $out_sids as $k=>$sidtmp ) {
			$sidtmp = trim( $sidtmp );
			//if( empty($sidtmp ) ) {
			//	$this->error( $listtid[$k] .'运单号为空.');
			//}
			$out_sidtmp[ $listtid[$k] ] = trim($sidtmp);
			$out_sidstr .= $sidtmp.',';
		}
		foreach( $tids as $k=>$tid ){
			if( empty( $out_sidtmp[ $tid ] ) ) {
				$error .= $tid . '运单号为空; ';
			}
		}
		
		$out_sidstr = trim( $out_sidstr , ',');
		//$hasOut_sid = D('Trade')->where( "out_sid in( $out_sidstr) ")->field('out_sid')->select( ) ;	//检查订单号是否存在
		
		if( ! empty($hasOut_sid) ) {
			$error .= '运单号已存在:';
			foreach ( $hasOut_sid as $out_sid ){
				$error .= $out_sid[out_sid].',' ;
			}
			
		}
		if(! empty( $error ) ) {
			$this->error( $error );
		}else {
			$d = D("Trade");
			
			foreach( $tids as $k=>$tid ){
				$data['out_sid'] = $out_sidtmp[ $tid ];
				$data['sysstatus'] = self::TRADE_STATUS_SHIPPED ;
				$d->where("tid={$tid}")->save($data);
				//$sql .= D()->_sql()."\n";
				
				$vo ['tid'] = $tid;
				$vo ['loguser'] = $_SESSION ['seller_nick'];
				$vo ['logtext'] = '系统保存运单号';
				$vo ['logtime'] = date ( 'Y-m-d H:i:s' );
				$vo ['role'] = 'administrator';
				D ( 'Tradelog' )->add ( $vo );
				
			}
			$this->tsendall();
			$this->ajaxReturn( '', '快递单号已更新，请卖家发货。' , 1);
		}
	}
	
	
	function tsidsaveone() {
		
		$tid 		=  $_POST[tid];
		$out_sid  	= $_POST[out_sid];
		if( empty( $tid ) || empty( $out_sid ) ){
			$this->error( '运单号参数错误' );
		}
		
		$hasOut_sid = D('Trade')->where( "out_sid='{$out_sid}' ")->field('out_sid')->select( ) ;	//检查订单号是否存在
	
		if( ! empty($hasOut_sid) ) {
			$error .= '运单号已存在:'.$out_sid ;
		}
		if(! empty( $error ) ) {
			$this->error( $error );
		}else {
			$d = D("Trade");
				
			 
				$data['out_sid'] = $out_sid;
				$data['sysstatus'] = self::TRADE_STATUS_SHIPPED ;
				$d->where("tid={$tid}")->save($data);
				//$sql .= D()->_sql()."\n";
	
				$vo ['tid'] = $tid;
				$vo ['loguser'] = $_SESSION ['seller_nick'];
				$vo ['logtext'] = '系统保存运单号';
				$vo ['logtime'] = date ( 'Y-m-d H:i:s' );
				$vo ['role'] = 'administrator';
				D ( 'Tradelog' )->add ( $vo );
	
			$this->tsendall();
			$this->ajaxReturn( '', '快递单号已更新，请卖家发货。' , 1);
		}
	}
	
	
	function tprint(){
		$tid =$_GET['tid'];
		$tinfo = D("Trade")->where("tid={$tid}")->find();
		//print_r($tinfo);
		//模板配置
		$tplid = $tinfo['systplid'];
		$tplinfo = D('Posttemplate')->where("id={$tplid}")->find();
		$tplcontent = $tplinfo['tplcontent'];
		$tplcontent = unserialize( $tplcontent ) ;
		
		//print_r( $tplcontent );
		
		$backimg  = __PUBLIC__ .'/images/print/'.$tplinfo['comid'].'.jpg';
		$backimginfo = getimagesize( $backimg );

		$printfileds = array(
			'buyer_nick'			,
			'receiver_name' 		,
			'receiver_state' 		,
			'receiver_city' 		,
			'receiver_district' 	,
			'receiver_address'		,
			'receiver_mobile'		,
			'receiver_phone' 		,
			'receiver_zip'			,
			'productinfo'			,
			'seller_memo'			,
			'seller_nick'			,
			'printdate'				,
				
		);
		$orderinfo = unserialize( $tinfo[orders] );
		$productinfo = '';
		if( is_array( $orderinfo ) ){
			foreach( $orderinfo[order] as $ordertmp ) {
				$productinfo.= "{$ordertmp['outer_sku_id']}({$ordertmp['num']}件)\\n";
			}
			$fproh = count( $orderinfo[order]) * 20 ;
		}
		//$productinfo.=$productinfo;
		//echo ( $productinfo );
		$tinfo[productinfo] = $productinfo;			//商品信息
		$tinfo[printdate] = date('Y-m-d');	//卖家备注
		
		$sellerfileds  = A('Seller')->sellerattrs;
		$sconfig = F('sconfig');
		
		$printcss = ".PrintArea {position :relative ; width : {$backimginfo[0]}px;height:{$backimginfo[1]}px; background: url({$backimg}) no-repeat;} \n";
		$printjs .= "LODOP.PRINT_INITA('{$tplinfo[tpltop]}mm','{$tplinfo[tplleft]}mm','{$tplinfo[tplwidth]}','{$tplinfo[tplheight]}','trade_print');"."\n";
		$printjs .= 'LODOP.SET_PRINT_PAGESIZE(1, \''.$tplinfo[tplwidth].'\', \''.$tplinfo[tplheight].'\', "CreateCustomPage");'."\n";
		foreach( $printfileds as $f) {
			if( empty( $tinfo[$f] )  ) continue;
			if( empty( $tplcontent[$f.'-marginleft'] )) continue;
			$fw = $tplcontent[$f.'-width'] ? $tplcontent[$f.'-width'] : '200';
			$printcss .= "#$f{ position:absolute;left:{$tplcontent[$f.'-marginleft']}px;top:{$tplcontent[$f.'-margintop']}px;font-size:{$tplcontent[$f.'-fontsize']}px;width:{$fw}px} \n";
			$printhtml .= "<div id='$f'>". $tinfo[ $f ] ." </div> \n";
			if( $tplcontent[$f.'-fontsize'] ) {
				$printjs .= "LODOP.SET_PRINT_STYLE(\"FontSize\",'{$tplcontent[$f.'-fontsize']}');\n";
			}
			
			$fh =  ($f == 'productinfo')  ? $fproh : 20 ;
			$printjs .= "LODOP.ADD_PRINT_TEXT(". $tplcontent[$f.'-margintop']. "," . $tplcontent[$f.'-marginleft'].", '{$fw}' , '{$fh}' , '". trim( $tinfo[ $f ] ) ."' );\n";
		}
		
		foreach( $sellerfileds as $k=>$f ) {
			if( empty( $tplcontent[$k.'-marginleft'] ) ) continue;
			if( empty( $sconfig[ $k ] ) ) continue;		
			$fw = $tplcontent[$k.'-width'] ? $tplcontent[$f.'-width'] : '200';
			$printcss .= "#$k{ position:absolute;left:{$tplcontent[$k.'-marginleft']}mm;top:{$tplcontent[$k.'-margintop']}mm;font-size:{$tplcontent[$k.'-fontsize']}px;width:{$fw}px} \n";
			$printhtml .= "<div id='$k'>". $sconfig[ $k ] ." </div> \n";
			if( $tplcontent[$k.'-fontsize'] ) {
				$printjs .= "LODOP.SET_PRINT_STYLE(\"FontSize\",'{$tplcontent[$k.'-fontsize']}');\n";
			}
			$printjs .= "LODOP.ADD_PRINT_TEXT(". $tplcontent[$k.'-margintop']. "," . $tplcontent[$k.'-marginleft'].", '{$fw}' , '30' , '".$sconfig[ $k ] ."' );\n";
			
		}
		$printtimejs = '$.post("'.U('tprinttime').'", {tid: '.$tid.' }, function(){ } );'.PHP_EOL;
		
		#$printjs .= "LODOP.ADD_PRINT_RECT(0, 0, {$backimginfo[0]}, {$backimginfo[1]}, 0, 1 );\n";
		$this->assign( 'backimg', $backimg);
		$this->assign( 'printcss', $printcss);
		$this->assign( 'printhtml', $printhtml);
		$this->assign( 'printjs', $printjs);
		$this->assign( 'printtimejs', $printtimejs);
		$this->display();
	}
	
	function tprintall ( ) {
		
		 
		$tidlist =  $_POST['tidlist'];
		
		$tidlist = explode( ';', $tidlist );
	 
		$tinfo = '';
		foreach ( $tidlist as $key=>$tid ) {
			if( empty( $tid )) continue;
			$tinfo = D("Trade")->where("tid={$tid}")->find();
			
			//模板配置
			
			$tplid = $tinfo['systplid'];
			$tplinfo = D('Posttemplate')->where("id={$tplid}")->find();
			$tplcontent = $tplinfo['tplcontent'];
			$tplcontent = unserialize( $tplcontent ) ;
			
			//print_r( $tplcontent );
			
			//$backimg  = __PUBLIC__ .'/images/print/'.$tplinfo['comid'].'.jpg';
			//$backimginfo = getimagesize( $backimg );
			
			$printfileds = array(
					'buyer_nick'			,
					'receiver_name' 		,
					'receiver_state' 		,
					'receiver_city' 		,
					'receiver_district' 	,
					'receiverstreet' 		,
					'receiver_address'		,
					'receiver_mobile'		,
					'receiver_phone' 		,
					'receiver_zip'			,
					'productinfo'			,
					'seller_memo'			,
					'seller_nick'			,
					'printdate'				,
			);
			
			$orderinfo = unserialize( $tinfo[orders] );
			$productinfo = '';
			if( is_array( $orderinfo ) ){
				foreach( $orderinfo[order] as $ordertmp ) {
					$productinfo.= "{$ordertmp['outer_sku_id']}({$ordertmp['num']}件)\\n";
				}
				$fproh = count( $orderinfo[order]) * 20 ;
			}
			//$productinfo.=$productinfo;
			//echo ( $productinfo );
			$tinfo[productinfo] = $productinfo;			//商品信息
			$tinfo[printdate] = date('Y-m-d');	//卖家备注
			
			$sellerfileds  = A('Seller')->sellerattrs;
			$sconfig = F('sconfig');
			
			//$printcss = ".PrintArea {position :relative ; width : {$backimginfo[0]}px;height:{$backimginfo[1]}px; background: url({$backimg}) no-repeat;} \n";
			//$printjs .= 'LODOP.PRINT_INIT("");'."\n";
			$printjs .= "LODOP.PRINT_INITA('{$tplinfo[tpltop]}mm','{$tplinfo[tplleft]}mm','{$tplinfo[tplwidth]}','{$tplinfo[tplheight]}','trade_print');"."\n";
			//$priintjs .= 'LODOP.SET_PRINT_MODE("POS_BASEON_PAPER",true);'."\n";
			$printjs .= 'LODOP.SET_PRINT_PAGESIZE(0, \''.$tplinfo[tplwidth].'\', \''.$tplinfo[tplheight].'\', "");'."\n";
			
			
			foreach( $printfileds as $f) {
				if( empty( $tinfo[ $f ] ) )continue;
				if( empty( $tplcontent[$f.'-marginleft'] )) continue;
				$fw = $tplcontent[$f.'-width'] ? $tplcontent[$f.'-width'] : '200';
				$printcss .= "#$f{ position:absolute;left:{$tplcontent[$f.'-marginleft']}px;top:{$tplcontent[$f.'-margintop']}px;font-size:{$tplcontent[$f.'-fontsize']}px;width:{$fw}px} \n";
				$printhtml .= "<div id='$f'>". $tinfo[ $f ] ." </div> \n";
				//$printjs .= "LODOP.SET_PRINT_STYLE();\n";
				if( $tplcontent[$f.'-fontsize'] ) {
					$printjs .= "LODOP.SET_PRINT_STYLE(\"FontSize\",'{$tplcontent[$f.'-fontsize']}');\n";
				}
				$fh =  ($f == 'productinfo')  ? $fproh : 20 ;
				$printjs .= "LODOP.ADD_PRINT_TEXT(". $tplcontent[$f.'-margintop']. "," . $tplcontent[$f.'-marginleft'].", '{$fw}' , '{$fh}' , '".$tinfo[ $f ] ."' );\n";
				//$printjs_a .= "LODOP.ADD_PRINT_TEXT(". $tplcontent[$f.'-margintop']. "," . $tplcontent[$f.'-marginleft'].", '200' , '30' , '".$tinfo[ $f ] ."' );\n";
			}
			
			foreach( $sellerfileds as $k=>$f ) {
				if( empty( $tplcontent[$k.'-marginleft'] ) ) continue;
				if( empty( $sconfig[ $k ] ) ) continue;
				$fw = $tplcontent[$k.'-width'] ? $tplcontent[$k.'-width'] : '200';
				$printcss .= "#$k{ position:absolute;left:{$tplcontent[$k.'-marginleft']}px;top:{$tplcontent[$k.'-margintop']}px;font-size:{$tplcontent[$k.'-fontsize']}px;width:{$fw}px} \n";
				$printhtml .= "<div id='$k'>". $sconfig[ $k ] ." </div> \n";
				if( $tplcontent[$k.'-fontsize'] ) {
					$printjs .= "LODOP.SET_PRINT_STYLE(\"FontSize\",'{$tplcontent[$k.'-fontsize']}');\n";
				}
				$printjs .= "LODOP.ADD_PRINT_TEXT(". $tplcontent[$k.'-margintop']. "," . $tplcontent[$k.'-marginleft'].", '{$fw}' , '30' , '".$sconfig[ $k ] ."' );\n";
				//$printjs_a .= "LODOP.ADD_PRINT_TEXT(". $tplcontent[$k.'-margintop']. "," . $tplcontent[$k.'-marginleft'].", '200' , '30' , '".$sconfig[ $k ] ."' );\n";
			}
			if( 0 == $key ) {
					$printjs_view = $printjs ;
			}
			 
			$printjs .= 'LODOP.PRINT();'."\n";
			
			$tids .= $tid.',';
			
		}
		$tids = trim($tids, ',');
		$printtimejs = '$.post("'.U('tprinttime').'", {tid: \''.$tids.'\' }, function(){ } );'.PHP_EOL;
		
		$this->assign( 'backimg', $backimg);
		$this->assign( 'printcss', $printcss);
		$this->assign( 'printhtml', $printhtml);
		$this->assign( 'printjs', $printjs);
		$this->assign( 'printjs_view', $printjs_view);
	//	$this->assign( 'printjs_a', $printjs_a);
		$this->assign( 'printtimejs', $printtimejs);
		$this->display();
		
	}
	function tfeedback () {
		$tid =  $_GET['tid'] ;
		if( $tid ) {
			$trade = D('Trade')->where("tid={$tid}")->find();
			$tradefeedback = D('Tradefeedback')->where("tid={$tid}")->select();

			$this->assign( 'tid', $tid );
			$this->assign( 'trade', $trade );
			$this->assign( 'tradefeedback', $tradefeedback );
			$this->display();
		}
	}
	
	function ttodo () {
		$tid = $_POST[tid];
		$systodo = D('Trade')->where("tid='{$tid}'")->field('systodo')->find();
	    
		$data[systodo] = $systodo['systodo'] == 1 ? 0 : 1 ;
		$vo = D('Trade')->where("tid='{$tid}'")->save( $data ) ;
		if ($vo) {
			$this->ajaxReturn ( $data[systodo], '成功', 1 );
		} else {
			$this->error ( D('Trade')->getError () );
		}
	}
	function tfeedbacksave () {
		$vo = D('Tradefeedback')->create($_POST);
		
		if( $vo ) {
			$seller_nick = $_SESSION['seller_nick'] ;
			$role = D("Seller")->where("nick='{$seller_nick}'")->getField('role');
		    $vo['seller_nick'] = $seller_nick;
		    $vo['role'] = $role;
		    $vo['addtime'] = date('Y-m-d H:i:s');
			D('Tradefeedback')->add( $vo );
			
			$tid = $vo['tid'];
			//$data['sysstatus'] = self::TRADE_STATUS_PENDING;
			D('Trade')->where("tid={$tid}")->save($data);
			
			
			$log['tid'] =  $tid;
			$log['loguser'] = $_SESSION['seller_nick'];
			$log['logtext'] = '待处理';
			$log['logtime'] = date('Y-m-d H:i:s');
			$log['role'] = 'administrator';
			D('Tradelog')->add( $log );
			
			
			$this->ajaxReturn($vo, '处理成功' , 1);
			
		}else{
			$this->error( D('Tradefeedback')->getError() );
		}
		
	}
	/**
	 * 取消待处理
	 */
	function tfeedbackcancel () {
		return '';
		$tid = $_POST['tid']; 
		//$data['sysstatus'] = self::TRADE_STATUS_PENDINGCANCELD ;
		$vo = D('Trade')->where("tid={$tid}")->save($data);
		if( $vo ) {
			
			$log['tid'] =  $tid;
			$log['loguser'] = $_SESSION['seller_nick'];
			$log['logtext'] = '取消待处理';
			$log['logtime'] = date('Y-m-d H:i:s');
			$log['role'] = 'administrator';
			D('Tradelog')->add( $log );
					
			$this->ajaxReturn( $vo, '已取消处理', 1);
		}else {
			$this->error( 'ERROR'.D('Trade')->getError() );
		}
		
	}
	
	/**
	 * 更新订单打印时间
	 */
	function tprinttime ( ) {
		$tids = $_POST['tid'];
		$data['sysprinttime'] = date('Y-m-d H:i:s');
		$vo = D('Trade')->where("tid in($tids)")->save($data);
		$this->ajaxReturn($vo, '', 1);
	}
	
	
	/**
	 * 修改订单的快递公司
	 */
	function tcompany () {
		$data['tid'] = $_POST['tid'] ;
		$data['systplid'] = $_POST['systplid'] ;
		
		if( $data ) {
			$com= D('Posttemplate')->where("id='{$data['systplid']}'")->field('comid')->find();
			$data['syscomid'] = $com['comid']; 
			$vo = D('Trade')->where("tid='{$data['tid']}'")->save( $data );
			$this->ajaxReturn( $vo, '修改成功', 1 );
		}
	}
	
	function t( ) { 
		$tid = '92242003188627' ;
		$tradeinfo  = D('Trade')->where( "tid=$tid")->find();
// 		echo D()->_sql();
		$seller = D('Sellersetting')->getSellerByNick( $tradeinfo['seller_nick'] );
// 		print_r( $seller );
		
		$access_token = $seller['access_token']; 
		$tc = new TopClient();
		$params = array (
				'method' => 'taobao.trade.fullinfo.get',
				'access_token' => $access_token ,
				'format' => TAO_RETURN_FORMAT,
		
				'v' => '2.0',
		
		
				'fields' => 'tid,status,orders.logistics_company',
				'tid' => $tid,
		);
		print_r( $params );
		$fullinfo = $tc->getHttpsResult( $params );
		//$fullinfo = curl_post( TAO_REST_HTTPS_URL, $params );
		
		print_r( $fullinfo );
		
	}
}